package com.vcredit.creditcat.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tempuri.SmsMessageData;

import com.vcredit.creditcat.framework.annotation.Log;
import com.vcredit.creditcat.model.SmsPsendLog;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.vcredit.creditcat.util.Constants;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.creditcat.vo.ArrayOfSmsMessageDataVo;
import com.vcredit.creditcat.vo.BatchSendSmsInputVo;

@Service
public class SMSService {

	@Autowired
	private SendSmsService sendSmsService;
	
	@Autowired
	private SmsPsendLogService smsPsendLogService;
	
	@Autowired
	private PersonInfoService personInfoService;
	
	@Log
	@Transactional
	public Boolean batchSendSms(BatchSendSmsInputVo param) throws Exception {
		Boolean result;
		String sameOperateNo = param.getSameOperateNo();
		int limit = param.getLimit();
		String smsContent = param.getSmsContent();
		String[] mobileArr = StringUtils.isNotBlank(param.getMobile()) ? param.getMobile().split(",") : null;
		ArrayList<String> mobiles = mobileArr == null ? null : new ArrayList<String>(Arrays.asList(mobileArr));
		int mobilesNum = mobiles == null ? 0 : mobiles.size();
		Date date = new Date();
		int start = 0;
		List<SmsMessageData> smsMessageData = new ArrayList<SmsMessageData>();
		// 用户总数
		Long total = personInfoService.querytotal();
		
		if(StringUtils.isEmpty(sameOperateNo)) {
			// 开启新的发送批次
			result = this.packageDataAndSend(start, limit, smsContent, smsMessageData, mobiles);
			if(result) {
				String dateStr = ConcurrentDateUtil.formatDateTime(date);
				Boolean status = this.getEndStatus(total, start, getSmsSendNum(mobilesNum,smsMessageData));
				smsPsendLogService.insert(
						new SmsPsendLog(dateStr, getSmsSendNum(mobilesNum,smsMessageData), start, smsContent, status, date, date));
			}
			return result;
		}
		
		// 根据批次号查询记录
		List<SmsPsendLog> list = smsPsendLogService.find(new SmsPsendLog(sameOperateNo, null, null));
		if(list.size()  == 0) {
			return false;
		}
		
		for (SmsPsendLog smspLog : list) {
			start += smspLog.getNum();
		}
		
		SmsPsendLog smsPsendLog = list.get(0);
		
		result = this.packageDataAndSend(start, limit, smsPsendLog.getContent(), smsMessageData, mobiles);
		if(result) {
			Boolean status = this.getEndStatus(total, start, getSmsSendNum(mobilesNum,smsMessageData));
			smsPsendLogService.insert(
					new SmsPsendLog(smsPsendLog.getSameOperateNo(), getSmsSendNum(mobilesNum,smsMessageData), start, smsContent, status, date, date));
			if(status)
				smsPsendLogService.updateStatusBySameOperateNo(smsPsendLog.getSameOperateNo());
		}
		return result;
	}
	
	private int getSmsSendNum(int mobileNum, List<SmsMessageData> smsMessageData) {
		if(smsMessageData.size() >= mobileNum)
			return smsMessageData.size() - mobileNum;
		return smsMessageData.size();
	}
	
	@Log
	private Boolean packageDataAndSend(int start, int limit, String smsContent, List<SmsMessageData> smsMessageData, List<String> mobiles) throws Exception {
		List<String> mobileList = personInfoService.findMobileByPage(start,limit);
		if(mobileList != null && !Utility.isNullOrEmpty(mobiles)) {
			mobileList.addAll(0, mobiles);
		}
		for (String mobile : mobileList) {
			SmsMessageData msgData = new SmsMessageData();
			msgData.setMobile(mobile);
			msgData.setMessageContent(smsContent);
			msgData.setOperationUserID(Constants.SMS_OPERATIONUSERID);
			msgData.setSignStr(Constants.SMS_SIGNSTR_VCODE);
			msgData.setSmsSource(Constants.SMS_SOURCE);
			msgData.setSmsTypeName(Constants.SMS_NOTIC_TYPENAME);
			msgData.setCustomerType(Constants.SMS_CUSTOMERYPTYPE);
			
			smsMessageData.add(msgData);
		}
		if(smsMessageData.size() == 0) {
			return false;
		}
		return sendSmsService.sendSmsList(new ArrayOfSmsMessageDataVo(smsMessageData));
	}
	
	private Boolean getEndStatus(Long total, int start, int limit) {
		if((start + limit) >= total)
			return true;
		return false;
	}

	public List<SmsPsendLog> queryList() {
		return smsPsendLogService.queryList();
	}
}
